import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/*You have to pay Tax no matter what if not enough money,
 *  transfer properties to bank*/
public class TaxUI extends JPanel {
	private BoardUi board;
	private JLabel label;
	private JButton btnPay, btnReturn;
	private Player p;
	private Block[] bl;
	private Tax tax;
	private Tax[] taxArray;
	private int collectTax;
	
	public TaxUI(Player currentPlayer, Tax tax_, Block[] blk, BoardUi board_) {
		this.p = currentPlayer;
		this.tax = tax_;
		this.bl=blk;
		this.taxArray = new Tax[2];
		this.board=board_;
	
		setBackground(new Color(230, 230, 250));
		setLayout(null);
		
		btnPay = new JButton("Pay");
		btnPay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pay();
			}
		});
		btnPay.setBounds(198, 62, 82, 23);
		add(btnPay);
		
		label = new JLabel("");
		label.setIcon(new ImageIcon(getClass().getResource(this.tax.getPicture())));
		label.setVisible(true);
		label.setBounds(0, 0, 200, 200);
		add(label);
		
		btnReturn = new JButton("Return");
		btnReturn.setEnabled(false);
		btnReturn.setVisible(false);
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				returnGame();
			}
		});
		btnReturn.setBounds(198, 96, 100, 23);
		add(btnReturn);
		
		this.fillTaxArray();
		this.checkTaxLocation();
	}
	
	public void returnGame(){
		board.getDiceUI().rolledDoubles();
	//	board.getGameButtonUI().enableEndTurnBtn();
		setVisible(false);
	}
	
	public void pay(){
		if(board.getCurrentPlayer().getMoney()>=collectTax){
		p.subtractMoney(collectTax);
		btnPay.setEnabled(false);
		btnPay.setVisible(false);
		btnReturn.setEnabled(true);
		btnReturn.setVisible(true);
		board.setCurrentPlayer();
		board.getPlayerPanelUI().updatePlayerMoney();
		}else{
			BankruptcyChecker bc = new BankruptcyChecker(p, bl);
			int worth=bc.checkWorth();
			
			if(worth>=collectTax){
				JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, just mortgage your properties");
			}else{
				bc.transferAssetsBank();
				board.getCurrentPlayer().setActive(false);
				JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, Out ka na!");
				btnReturn.setVisible(true);
			}	
			}
	}
	
	public void fillTaxArray() {
		int taxCounter = 0;
		for(int i=0;i<this.bl.length;i++){
			if(this.bl[i] instanceof Tax){
				this.taxArray[taxCounter]=(Tax)bl[i];
				taxCounter++;
			}
		}	
	}
	
	public void checkTaxLocation(){

		for(int i=0; i<this.taxArray.length; i++){
			if(this.taxArray[i].getTaxLocation()==this.tax.getTaxLocation()){//match fist
				if(this.tax.getTaxLocation()==1){
				this.collectTax=200;
				}else if(this.tax.getTaxLocation()==2){
				this.collectTax=75;
				}
			}
		}
	}
}
